package stack_queue.leetcode;

//Num155
//设计一个支持 push ，pop ，top 操作，并能在常数时间内检索到最小元素的栈

import java.util.Stack;

public class MinStack {
    // s1是实际存储元素的栈
    private Stack<Integer> s1=new Stack<>();
    //s2是存储最小值的栈
    private Stack<Integer> s2=new Stack<>();

    public MinStack() {

    }

    public void push(int val) {
        s1.push(val);
        if(s2.isEmpty()){
            s2.push(val);
        }else {
            // 比较当前val和s2的栈顶元素，将最小值入栈
            int temp=s2.peek();
            s2.push(Math.min(temp,val));
        }
    }

    public void pop() {
        s1.pop();
        s2.pop();
    }

    public int top() {
        return s1.peek();
    }

    public int getMin() {
        return s2.peek();
    }
}
